package com.test.qnews.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.test.qnews.R;
import com.test.qnews.Recommend;
import com.test.qnews.RecommendDao;
import com.test.qnews.activity.DetailsActivity;
import com.test.qnews.adapter.CommonAdapter;
import com.test.qnews.beans.GsonImgBean;
import com.test.qnews.beans.NetDataBean;
import com.test.qnews.db.DaoSingleton;
import com.test.qnews.volley.VolleySingleton;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 16/1/8.
 */
public class MilitaryFragment extends Fragment{
    private Context context;
    private CommonAdapter commonAdapter;
    private ListView listView;
    private List<NetDataBean> data;
    private String url = "http://zzd.sm.cn/appservice/api/v1/channel/1105405272?client_os=android&client_version=2.1.0.1&bid=800&m_ch=004&city=0411&sn=ade627ae9c8b3165f8beab83650296fd&content_cnt=20&recoid=&ftime=0&method=new&count=20";

    private List<Recommend> recommends = new ArrayList<>();
    private RecommendDao recommendDao = DaoSingleton.getInstance().getRecommendDao();
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_recommend, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        listView = (ListView) view.findViewById(R.id.lv_recommend);

        data();
    }

    private void data() {

        VolleySingleton.getInstance();
        VolleySingleton.getInstance().addRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                data = new ArrayList<>();
                GsonImgBean gsonBean = gson.fromJson(response, GsonImgBean.class);

                for (int i = 0; i < gsonBean.getData().getArticle().size(); i++) {
                    NetDataBean netDataBean = new NetDataBean();
                    netDataBean.setSource_name(gsonBean.getData().getArticle().get(i).getSource_name());
                    netDataBean.setTitle(gsonBean.getData().getArticle().get(i).getTitle());
                    for (int j = 0; j < gsonBean.getData().getArticle().get(i).getImage().size(); j++) {
                        netDataBean.setImageUrl(gsonBean.getData().getArticle().get(i).getImage().get(0).getUrl());
                    }
                    netDataBean.setContent(gsonBean.getData().getArticle().get(i).getContent());
                    netDataBean.setItem_type(gsonBean.getData().getArticle().get(i).getItem_type());
                    data.add(netDataBean);


                    Recommend recommend = new Recommend();
                    recommend.setTitle(data.get(i).getTitle());
                    recommend.setContent(data.get(i).getContent());
                    recommend.setImageUrl(data.get(i).getImageUrl());
                    recommend.setItem_type(data.get(i).getItem_type());
                    recommends.add(recommend);
                }


                commonAdapter = new CommonAdapter(data, context);
                listView.setAdapter(commonAdapter);

                listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        Intent intent = new Intent(getActivity(), DetailsActivity.class);
                        intent.putExtra("pos", position);
                        intent.putExtra("data", (Serializable) data);
                        startActivity(intent);
                    }
                });

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                commonAdapter = new CommonAdapter(context,search());
                listView.setAdapter(commonAdapter);
            }
        });
    }


    private List<Recommend> search() {
        if (recommendDao != null) {
            return recommendDao.queryBuilder().list();
        } else {
            return null;
        }
    }
}
